/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.DoanTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class DoanTau_DAO {
    
    public static String Get_MaDT() throws SQLException{
        String MaDT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "DT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaDT = Rs.getString(1);
        }
        return MaDT;
    }
    
    public static ArrayList<DoanTau_DTO> GetALL()
    {
        ArrayList<DoanTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from DoanTau";
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                DoanTau_DTO DoanTau = new DoanTau_DTO();
                DoanTau.setMaDT(rs.getString("MaDT"));
                DoanTau.setTenDT(rs.getString("TenDT"));
                DoanTau.setLoaiDT(rs.getInt("LoaiDT"));
                DoanTau.setHangSX(rs.getString("HangSX"));
                DoanTau.setNgayHD(rs.getDate("NgayHD"));
                ds.add(DoanTau);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    public static boolean Insert(DoanTau_DTO DoanTau)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayHD = fmt.format(DoanTau.getNgayHD());
            String sql = "Insert DoanTau Values('"+DoanTau.getMaDT()+"','"+DoanTau.getTenDT()+"','"+DoanTau.getLoaiDT()+"','"+DoanTau.getHangSX()+"','"+NgayHD+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(DoanTau_DTO DoanTau)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayHD = fmt.format(DoanTau.getNgayHD());
            String sql = "Update DoanTau Set TenDT = '"+ DoanTau.getTenDT() +"', LoaiDT = "+DoanTau.getLoaiDT()+", HangSX = '"+DoanTau.getHangSX()+"', NgayHD = '"+ NgayHD +"' where MaDT =  '"+DoanTau.getMaDT()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maDT)
    {
        boolean flag = false;
        try {
            String sql = "Delete from DoanTau where MaDT = '"+maDT+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
